<template>

  <el-container>
    <el-header style="display: flex;justify-content: space-between;align-items: center;">
      <h3>测试管理</h3>
      <!-- <div class="time">2024-02-01</div> -->
    </el-header>
    <el-main>

      <div class="app-container">
        <el-row :gutter="24">
          <!-- 搜索框 queryParams.需要搜索的字段 -->
          <el-form :model="queryParams" label-position="left" style="margin:15px;" inline ref="queryForm"
            v-show="showSearch" @submit.prevent>
            <el-form-item label="标题" prop="noticeTitle">
              <el-input v-model="queryParams.noticeTitle" placeholder="请输入标题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button icon="search" type="primary" @click="handleQuery">搜索</el-button>
              <el-button icon="refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>

        <!-- 工具按钮 -->
        <el-row :gutter="10" class="mb8" style="margin-bottom: 20px;">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="plus" @click="AddDialogVisible = true">
              添加
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" :disabled="multiple" plain icon="delete" @click="handleDelete">
              删除
            </el-button>
          </el-col>
        </el-row>

        <!-- 表格渲染  prop="对应的字段"-->
        <el-table v-loading="loading" :data="dataList" ref="tableRef" highlight-current-row
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />

          <el-table-column prop="noticeTitle" label="标题" align="center" :show-overflow-tooltip="true" />
          <el-table-column prop="noticeContent" label="内容" align="center" :show-overflow-tooltip="true" />
          <el-table-column prop="noticeImg" width="350" label="图片" align="center">
            <template #default="scope">
              <el-image preview-teleported :hide-on-click-modal="true" lazy style="width:100px;" fit="contain"
                :src="scope.row.noticeImg?.split(',')[0]" :preview-src-list="scope.row.noticeImg?.split(',')">
                <div><el-icon :size="15">
                    <document />
                  </el-icon></div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="noticeSort" label="排序" align="center" sortable />

          <el-table-column label="操作" width="350" fixed="right">
            <template #default="scope">
              <el-button type="primary" size="small" icon="edit" @click="handleUpdate(scope.row)">编辑</el-button>
              <el-button type="danger" size="small" icon="delete" @click="handleDelete(scope.row)">删除</el-button>
              <el-button size="small" icon="view" @click="handleDetail(scope.row)">查看</el-button>
            </template>
          </el-table-column>

        </el-table>
        <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
          @pagination="getList" />
      </div>

    </el-main>
  </el-container>


  <!-- 添加 -->
  <AddDialog v-model="AddDialogVisible" :done="() => resetQuery()"></AddDialog>
  <!-- 编辑 -->
  <EditDialog v-model="EditDialogVisible" :data="EditDialogRow" :done="() => resetQuery()"></EditDialog>
  <!-- 详情 -->
  <DetailDialog v-model="DetailDialogVisible" :data="DetailDialogRow" :done="() => resetQuery()"></DetailDialog>

</template>

<script lang="ts" setup>
import { computed, ref, reactive, toRefs } from 'vue'

import { ElMessageBox } from 'element-plus'
// import { noticeList, delNotice } from '@/api/business/Advertisement/Notices/notice.js'
import AddDialog from "./components/AddDialog.vue";
import EditDialog from "./components/EditDialog.vue";
import DetailDialog from "./components/DetailDialog.vue";

const AddDialogVisible = ref(false);
const EditDialogVisible = ref(false);
const EditDialogRow = ref({});
const DetailDialogVisible = ref(false);
const DetailDialogRow = ref({});

// 选中categoryId数组数组
const ids = ref([])
// 非单选禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 数据列表
const dataList = ref([])
// 总记录数
const total = ref(0)
// 是否加载
const loading = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    noticeTitle: ""
  },
})
const { queryParams } = toRefs(data)

// 业务参数
let noticeData = ref([
  {
    "noticeId": 1,
    "noticeGuid": "1711389367018328064",
    "noticeTitle": "公告",
    "noticeContent": "这是一条公告",
    "noticeSort": 1,
    "noticeImg": "https://drive.aerwen.net/d/aerwen/%E5%9B%BE%E5%BA%8A/1710499419607.png",
  },
  {
    "noticeId": 1,
    "noticeGuid": "1711389367018328064",
    "noticeTitle": "公告",
    "noticeContent": "这是一条公告",
    "noticeSort": 1,
    "noticeImg": "https://drive.aerwen.net/d/aerwen/%E5%9B%BE%E5%BA%8A/1710499419607.png",
  },
  {
    "noticeId": 1,
    "noticeGuid": "1711389367018328064",
    "noticeTitle": "公告",
    "noticeContent": "这是一条公告",
    "noticeSort": 1,
    "noticeImg": "https://drive.aerwen.net/d/aerwen/%E5%9B%BE%E5%BA%8A/1710499419607.png",
  }
])

// 业务方法





//基础方法

// 查询数据
function getList() {
  loading.value = false

  dataList.value = noticeData.value;

  // noticeList(queryParams.value).then((res) => {
  //   if (res.code == 200) {
  //     loading.value = false;
  //     dataList.value = res.data.result;
  //     total.value = res.data.totalNum;
  //   }
  // });
}


// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置查询操作 */
function resetQuery() {
  // proxy.resetForm('queryForm')
  handleQuery()
}
/** 搜索按钮操作 */
function handleQuery() {
  getList()
}
/** 删除按钮操作 */
function handleDelete(row) {
  const Ids = row.noticeId || ids.value

  ElMessageBox.confirm("是否确认删除？", "系统提示", {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: "warning",
  })
    .then(function () {
      return delNotice(Ids)
    })
    .then(() => {
      handleQuery()
      EessamgeBox.msgSuccess("删除成功")
    })
    .catch(() => { })
}



// 修改
function handleUpdate(row) {
  EditDialogVisible.value = true
  EditDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailDialogVisible.value = true
  DetailDialogRow.value = row
}

handleQuery()

interface User {
  date: string
  name: string
  address: string
}

const search = ref('')
const filterTableData = computed(() =>
  tableData.filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
)
const handleEdit = (index: number, row: User) => {
  console.log(index, row)
}
// const handleDelete = (index: number, row: User) => {
//   console.log(index, row)
// }

const tableData: User[] = [
  {
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'John',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Morgan',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-01',
    name: 'Jessy',
    address: 'No. 189, Grove St, Los Angeles',
  },
]
</script>

<style scoped>
.peoplebox {
  display: flex;

  width: 95%;
  height: 100vh;
  background-color: rgba(227, 227, 202, 0.4);
  backdrop-filter: blur(15px);
  -webkit-backdrop-filter: blur(15px);
  border: 0.8px solid rgba(255, 255, 255, 0.18);
  box-shadow: rgba(142, 142, 142, 0.19) 0px 6px 15px 0px;
  -webkit-box-shadow: rgba(142, 142, 142, 0.19) 0px 6px 15px 0px;
  border-radius: 13px;
  -webkit-border-radius: 13px;
  padding: 20px;
}
</style>